package edu.fjut.se1603.yuan.dao.impl;

import edu.fjut.se1603.yuan.dao.BaseDao;
import edu.fjut.se1603.yuan.dao.PetStoreDao;
import edu.fjut.se1603.yuan.entity.PetOwner;
import edu.fjut.se1603.yuan.entity.PetStore;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class PetStoreDaoImpl extends BaseDao implements PetStoreDao {


    @Override
    public double getStoreMoney(Integer petStroeId) {
        double money=0;
        try {
            PreparedStatement preparedStatement=conn.prepareStatement("select MONEY from pet_store where ID=?");
            preparedStatement.setInt(1,petStroeId);
            ResultSet set=preparedStatement.executeQuery();
            if(set.next()){
                money=set.getDouble("MONEY");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return money;
    }

    @Override
    public List<PetStore> getAllStore() {
        List<PetStore> petStores = null;
        try {
            Statement statement = conn.createStatement();
            ResultSet set = statement.executeQuery("select * from pet_store");

            petStores = new ArrayList<>();
            while (set.next()) {
                int id = set.getInt("ID");
                String name = set.getString("NAME");
                double money = set.getDouble("MONEY");
                petStores.add(new PetStore(id, name,null,money));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return petStores;
    }

    @Override
    public int updateStoreMoney(Integer petStoreId, double money) {
        int flag=0;
        try {
            PreparedStatement preparedStatement=conn.prepareStatement("update pet_store set MONEY=? where ID=?");
            preparedStatement.setDouble(1,money);
            preparedStatement.setInt(2,petStoreId);
            flag=preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return flag;
    }
}
